#
#        Python GUI - Buttons - Swing
#

import javax.swing as swing
from GButtons import Button as GButton
from StdFonts import system_font
from JHacks import *
from Controls import to_html,from_html,_j_ActionCallback

_j_halign={"left":swing.AbstractButton.LEFT,"right":swing.AbstractButton.RIGHT,"center":swing.AbstractButton.CENTER,"centre":swing.AbstractButton.CENTER}

class Button(GButton):
    
    def __init__(self, title="Button", font=system_font, **kwds):
        self._just=kwds.get("just","center")
        self._title=title
        self._j_obj=swing.JButton(to_html(title,self._just))
        
        self._actioncallback=_j_ActionCallback(self)
        self._j_obj.addActionListener(self._actioncallback)
        
        GButton.__init__(self,title=title,font=font,**kwds)
    
    def activate(self):
        self._j_obj.doClick(100)
    
    def trigger(self): #Called by the _j_ActionCallback()
        self.do_action()
    
    def set_style(self,style):
        pass
    
    def get_style(self):
        return None
    
    def set_title(self,title):
        self._title=title
        self._j_obj.setText(to_html(title,self._just))
    
    def get_title(self):
        return from_html(self._j_obj.getText())
    
    def get_just(self):
        return self._just
    
    def set_just(self,just):
        self._just=just
        self._j_obj.setText(to_html(self._title,self._just))
        self._j_obj.setHorizontalAlignment(_j_halign[self._just])

